package Converter;

import VO.Klasse;
import DB.DBehandler;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *KlasseConverter; bruker DBehandler
 */
@ManagedBean
public class KlasseConverter implements Converter {
    private DBehandler db = new DBehandler();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        String trinnString = String.valueOf(value.charAt(0));
        int trinn = Integer.parseInt(trinnString);
        String klasse = String.valueOf(value.charAt(2));
        ArrayList<Klasse> k = db.hentKlasser();
        for (Klasse a : k) {
            if (a.getKlasse().equals(klasse) && a.getTrinn() == trinn) {
                return a;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Klasse k = (Klasse) value;
        return k.toString();
    }
}
